package per.turtle.gen.domain;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.ArrayUtils;
import per.turtle.gen.constants.GenConstants;
import per.turtle.common.core.web.entity.BaseEntity;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author ruoyi
 */
@Schema(name = "GenTable", description = "业务表")
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("gen_table")
@ToString
public class GenTable extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @Schema(description = "编号")
    private Long tableId;

    @NotBlank(message = "表名称不能为空")
    @Schema(description = "表名称")
    private String tableName;

    @NotBlank(message = "表描述不能为空")
    @Schema(description = "表描述")
    private String tableComment;

    @Schema(description = "关联父表的表名")
    private String subTableName;

    @Schema(description = "本表关联父表的外键名")
    private String subTableFkName;

    @NotBlank(message = "实体类名称不能为空")
    @Schema(description = "实体类名称(首字母大写)")
    private String className;

    @Schema(description = "使用的模板（crud:单表操作;tree:树表操作;sub:主子表操作;）")
    private String tplCategory;

    @NotBlank(message = "生成包路径不能为空")
    @Schema(description = "生成包路径")
    private String packageName;

    @NotBlank(message = "生成模块名不能为空")
    @Schema(description = "生成模块名")
    private String moduleName;

    @NotBlank(message = "生成业务名不能为空")
    @Schema(description = "生成业务名")
    private String businessName;

    @NotBlank(message = "生成功能名不能为空")
    @Schema(description = "生成功能名")
    private String functionName;

    @NotBlank(message = "作者不能为空")
    @Schema(description = "生成作者")
    private String functionAuthor;

    @Schema(description = "生成代码方式（0:zip压缩包;1:自定义路径）")
    private String genType;

    @Schema(description = "生成路径（不填默认项目路径）")
    private String genPath;

    @Schema(description = "主键信息")
    private GenTableColumn pkColumn;

    @Schema(description = "子表信息")
    private GenTable subTable;

    @Schema(description = "表列信息")
    @Valid
    private List<GenTableColumn> columns;

    @Schema(description = "其它生成选项")
    private String options;

    @Schema(description = "树编码字段")
    private String treeCode;

    @Schema(description = "树父编码字段")
    private String treeParentCode;

    @Schema(description = "树名称字段")
    private String treeName;

    @Schema(description = "上级菜单ID字段")
    private String parentMenuId;

    @Schema(description = "上级菜单名称字段")
    private String parentMenuName;

    public static boolean isCrud(String tplCategory) {
        return tplCategory != null && StrUtil.equals(GenConstants.TPL_CRUD, tplCategory);
    }

    public static boolean isSub(String tplCategory) {
        return tplCategory != null && StrUtil.equals(GenConstants.TPL_SUB, tplCategory);
    }

    public static boolean isSuperColumn(String tplCategory, String javaField) {
        if (isTree(tplCategory)) {
            return StrUtil.equalsAnyIgnoreCase(javaField, ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
        }
        return StrUtil.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
    }

    public static boolean isTree(String tplCategory) {
        return tplCategory != null && StrUtil.equals(GenConstants.TPL_TREE, tplCategory);
    }

    public boolean isCrud() {
        return isCrud(this.tplCategory);
    }

    public boolean isSub() {
        return isSub(this.tplCategory);
    }

    public boolean isSuperColumn(String javaField) {
        return isSuperColumn(this.tplCategory, javaField);
    }

    public boolean isTree() {
        return isTree(this.tplCategory);
    }

}
